package com.offcn.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public abstract class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //接收方法
        String method = req.getParameter("method");
        //利用反射处理请求 获取BookServlet的class方法
        Class<? extends BaseServlet > clazz = this.getClass();
        try {
            Method mt = clazz.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            mt.setAccessible(true);
            mt.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
